<?php

namespace app\controllers\h5\execute;

use app\controllers\h5\base\H5Controller;
use app\models\user\UserAddress as Model;
use app\models\City;

/**
 * Class AddressController
 * @package app\controllers\h5\execute
 */
class AddressController extends H5Controller
{
    public function actionList()
    {
        $list = Model::find()
            ->where(['user_id' => $this->user->id])
            ->andWhere(['status' => Model::$statusMapping['OK']])
            ->with(['city'])
            ->orderBy(['is_default' => SORT_DESC, 'id' => SORT_DESC])
            ->asArray()
            ->all();

        foreach ($list as &$item) {
            $item['nickname'] = base64_decode($item['nickname']);
        }

        return $this->success($list);
    }

    public function actionSetDefault()
    {
        $id = $this->post('id');
        $value = $this->post('value');

        if ($value == '1') {
            Model::updateAll(['is_default' => 0], ['user_id' => $this->user->id]);
        }

        $model = Model::findOne(['id' => $id, 'user_id' => $this->user->id]);
        if ($model) {
            $model->is_default = $value;
            if ($model->save()) {
                return $this->success();
            }
        }

        return $this->fail();
    }

    public function actionDelete()
    {
        $id = $this->post('id');

        $model = Model::findOne(['id' => $id, 'user_id' => $this->user->id]);
        if ($model) {
            // $model->status = Model::$statusMapping['DELETE'];
            $model->status = Model::$statusMapping['CLOSE'];
            if ($model->save()) {
                return $this->success();
            }
        }

        return $this->fail();
    }

    public function actionView()
    {
        $id = $this->post('id');

        $model = Model::find()
            ->where(['id' => $id, 'user_id' => $this->user->id])
            ->with(['city'])
            ->asArray()
            ->one();
        if ($model) {
            $model['nickname'] = base64_decode($model['nickname']);
            return $this->success($model);
        }

        return $this->fail();
    }

    public function actionEdit()
    {
        $data = $this->post();

        // -2 => order add address
        // see views/order/components/Address.vue

        if (isset($data['id']) && $data['id'] > 0) {
            $model = Model::findOne(['id' => $data['id'], 'user_id' => $this->user->id]);
        } else {
            $model = new Model();
        }

        // check city
        $city = City::find()
            ->where(['province_id' => $data['province_id']])
            ->andWhere(['city_id' => $data['city_id']]);
        if ($data['area_id']) {
            $city->andWhere(['area_id' => $data['area_id']]);
        }
        $city = $city->one();
        if (!$city) {
            return $this->fail('地区选择错误');
        }

        $data['table_id'] = $city->id;
        $data['region_id'] = $city->region_id;
        $data['province_id'] = $city->province_id;
        $data['city_id'] = $city->city_id;
        $data['area_id'] = $city->area_id;

        $data['user_id'] = $this->user->id;

        // default address
        if ($data['is_default'] == 1) {
            Model::updateAll(['is_default' => 0], ['user_id' => $data['user_id']]);
        }

        $data['nickname'] = base64_encode($data['nickname']);

        $data = ['UserAddress' => $data];
        if ($model->load($data) && $model->save()) {
            return $this->success();
        }

        return $this->fail('操作失败');
    }
}